package com.lgkj.ui;

import android.Manifest;
import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.lgkj.MainActivity;
import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.CountDownTimerUtil;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.utils.permission.PermissionListener;
import com.lgkj.utils.permission.PermissionUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 启动页
 */
public class StartActivity extends BaseActivity {
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    public AMapLocationClient mLocationClient = null;
    private long times = 500;
    private String notice = "心中有阳光，脚底有力量！心中有阳光，脚底有力量！心中有阳光，脚底有力量！";
    private boolean isStart = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_start;
    }

    @Override
    public void initView() {
        upDatatoken();
        updata();
        Log.i(TAG, "initView:token" + System.currentTimeMillis());
    }

    private CountDownTimerUtil countDownTimer = new CountDownTimerUtil(times, 1000) {
        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
            autoLogin();
            startActivity(new Intent(StartActivity.this, MainActivity.class));
            finish();
        }
    };

    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    Log.d(TAG, "onLocationChanged: " + aMapLocation.toString());
                    SharedPreferencesUtils.setParam(StartActivity.this, "adress", aMapLocation.getPoiName() + aMapLocation.getStreet() + aMapLocation.getStreetNum());
                    SharedPreferencesUtils.setParam(StartActivity.this, "city", aMapLocation.getCity() + "");
                    SharedPreferencesUtils.setParam(StartActivity.this, "lat", aMapLocation.getLatitude() + "");
                    SharedPreferencesUtils.setParam(StartActivity.this, "lng", aMapLocation.getLongitude() + "");
                    getstartImg(aMapLocation.getLatitude() + "", aMapLocation.getLongitude() + "");
                } else {
                    //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    };
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    public void initData() {
        Log.i(TAG, "initData:sid:" + System.currentTimeMillis());
        PermissionUtil permissionUtil = new PermissionUtil(StartActivity.this);
        permissionUtil.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionListener() {
            @Override
            public void onGranted() {
                getLoal();
            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                toastLong("请先开启定位权限", 0);
                countDownTimer.setCountdownInterval(3000);
                countDownTimer.setMillisInFuture(1000);
                countDownTimer.start();

            }

            @Override
            public void onShouldShowRationale(List<String> deniedPermission) {
//                countDownTimer.setCountdownInterval(3000);
//                countDownTimer.setMillisInFuture(1000);
//                countDownTimer.start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void getLoal() {
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setOnceLocationLatest(true);
        mLocationClient = new AMapLocationClient(getBaseContext());
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
    }

    private void getstartImg(String lat, String lng) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(StartActivity.this));
        map.put("lat", lat + "");
        map.put("lng", lng + "");
        HttpUtils.postHttp(map, CommonConstant.startimg, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (0 == jsonObject.optInt("code")) {
                    String url = jsonObject.optJSONObject("data").optString("img_id");
                    if (isStart)
                        CommonUtils.LoadImage(StartActivity.this, ivBg, url, 0);

                }
                countDownTimer.start();
            }

            @Override
            public void errorHttp(String msg) {
                countDownTimer.start();
            }
        });
    }


    @OnClick(R.id.tv_start)
    public void onClick() {
        isStart = false;
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer.setCountdownInterval(3000);
            countDownTimer.setMillisInFuture(times);
            countDownTimer.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
